package jnpf.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_DuBanRenWuEntity;
import jnpf.mapper.ZXGL_DuBanRenWuMapper;
import jnpf.model.zxgl_dubanrenwu.ZXGL_DuBanRenWuPagination;
import jnpf.permission.service.AuthorizeService;
import jnpf.service.ZXGL_DuBanRenWuService;
import jnpf.util.DateUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ZXGL_DuBanRenWu
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-21
 */
@Service
public class ZXGL_DuBanRenWuServiceImpl extends ServiceImpl<ZXGL_DuBanRenWuMapper, ZXGL_DuBanRenWuEntity> implements ZXGL_DuBanRenWuService {


    @Autowired
    private UserProvider userProvider;

    @Autowired
    private AuthorizeService authorizeService;

    @Resource
    PermissionsConfig permissionsConfig;

    @Override
    public List<ZXGL_DuBanRenWuEntity> getList(ZXGL_DuBanRenWuPagination zXGL_DuBanRenWuPagination) {

        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_DuBanRenWuEntity> zXGL_DuBanRenWuQueryWrapper = new QueryWrapper<>();
        //TODO 增加权限逻辑
        UserInfo userInfo = userProvider.get();
        if (!userInfo.getIsAdministrator()) {
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds == null || (StringUtils.isNotBlank(alldataRoleId) && !roleIds.contains(alldataRoleId))) {
                String userId = userInfo.getUserId();
                //任务督办：创建人看本人数据；创建人部门负责人看本部门数据；创建人部门主管领导看所管理部门数据；
                // 主责人看自己负责项目数据；主责部门负责人看本部门负责项目数据；主责部门主管领导看所管理部门负责数据；
                zXGL_DuBanRenWuQueryWrapper.lambda().and(wrapper -> wrapper
                        .eq(ZXGL_DuBanRenWuEntity::getCreatorUserId, userId)//创建人看本人数据
                        .or()
                        .eq(ZXGL_DuBanRenWuEntity::getDepartmentManager, userId)//创建人部门负责人看本部门数据
                        .or()
                        .eq(ZXGL_DuBanRenWuEntity::getDepartmentHeadleader, userId) //创建人部门主管领导看所管理部门数据
                        .or()
                        .eq(ZXGL_DuBanRenWuEntity::getZzr, userId)//主责人看自己负责项目数据
                        .or()
                        .eq(ZXGL_DuBanRenWuEntity::getDepartmentManagerZzbm, userId)//主责部门负责人看本部门负责项目数据
                        .or()
                        .eq(ZXGL_DuBanRenWuEntity::getDepartmentHeadleaderZzbm, userId) //主责部门主管领导看所管理部门负责数据
                );

            }
        }
        int zXGL_DuBanRenWuNum = 0;
        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getFqr())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().eq(ZXGL_DuBanRenWuEntity::getFqr, zXGL_DuBanRenWuPagination.getFqr());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getXmmc())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().like(ZXGL_DuBanRenWuEntity::getXmmc, zXGL_DuBanRenWuPagination.getXmmc());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getFqrq())) {
            zXGL_DuBanRenWuNum++;
            List<String> FqrqList = zXGL_DuBanRenWuPagination.getFqrq();
            Long fir = Long.valueOf(FqrqList.get(0));
            Long sec = Long.valueOf(FqrqList.get(1));

            zXGL_DuBanRenWuQueryWrapper.lambda().ge(ZXGL_DuBanRenWuEntity::getFqrq, new Date(fir))
                    .le(ZXGL_DuBanRenWuEntity::getFqrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getRwmc())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().like(ZXGL_DuBanRenWuEntity::getRwmc, zXGL_DuBanRenWuPagination.getRwmc());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getZzr())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().eq(ZXGL_DuBanRenWuEntity::getZzr, zXGL_DuBanRenWuPagination.getZzr());
        }

        if (AllIdList.size() > 0) {
            zXGL_DuBanRenWuQueryWrapper.lambda().in(ZXGL_DuBanRenWuEntity::getId, AllIdList);
        }
        /**
         * 数据权限 此处手动写入菜单id
         */
        String moduleId = "673058794472765061";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_DuBanRenWuQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_DuBanRenWuQueryWrapper = (QueryWrapper<ZXGL_DuBanRenWuEntity>) authorizeService.getCondition(zXGL_DuBanRenWuQueryWrapper, userProvider.get(), moduleId);
//        }
        //排序
//        if (StringUtil.isEmpty(zXGL_DuBanRenWuPagination.getSidx())) {
//            zXGL_DuBanRenWuQueryWrapper.lambda().orderByDesc(ZXGL_DuBanRenWuEntity::getFqr);
//        } else {
//            try {
//                String sidx = zXGL_DuBanRenWuPagination.getSidx();
//                ZXGL_DuBanRenWuEntity zXGL_DuBanRenWuEntity = new ZXGL_DuBanRenWuEntity();
//                Field declaredField = zXGL_DuBanRenWuEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_DuBanRenWuQueryWrapper = "asc".equals(zXGL_DuBanRenWuPagination.getSort().toLowerCase()) ? zXGL_DuBanRenWuQueryWrapper.orderByAsc(value) : zXGL_DuBanRenWuQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
        zXGL_DuBanRenWuQueryWrapper.lambda().orderByDesc(ZXGL_DuBanRenWuEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_DuBanRenWuEntity> page = new Page<>(zXGL_DuBanRenWuPagination.getCurrentPage(), zXGL_DuBanRenWuPagination.getPageSize());
            IPage<ZXGL_DuBanRenWuEntity> userIPage = this.page(page, zXGL_DuBanRenWuQueryWrapper);
            return zXGL_DuBanRenWuPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_DuBanRenWuEntity> list = new ArrayList();
            return zXGL_DuBanRenWuPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_DuBanRenWuEntity> getTypeList(ZXGL_DuBanRenWuPagination zXGL_DuBanRenWuPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_DuBanRenWuEntity> zXGL_DuBanRenWuQueryWrapper = new QueryWrapper<>();
        int zXGL_DuBanRenWuNum = 0;
        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getFqr())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().eq(ZXGL_DuBanRenWuEntity::getFqr, zXGL_DuBanRenWuPagination.getFqr());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getXmmc())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().like(ZXGL_DuBanRenWuEntity::getXmmc, zXGL_DuBanRenWuPagination.getXmmc());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getFqrq())) {
            zXGL_DuBanRenWuNum++;
            List<String> FqrqList = zXGL_DuBanRenWuPagination.getFqrq();
            Long fir = Long.valueOf(FqrqList.get(0));
            Long sec = Long.valueOf(FqrqList.get(1));

            zXGL_DuBanRenWuQueryWrapper.lambda().ge(ZXGL_DuBanRenWuEntity::getFqrq, new Date(fir))
                    .le(ZXGL_DuBanRenWuEntity::getFqrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getRwmc())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().like(ZXGL_DuBanRenWuEntity::getRwmc, zXGL_DuBanRenWuPagination.getRwmc());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanRenWuPagination.getZzr())) {
            zXGL_DuBanRenWuNum++;
            zXGL_DuBanRenWuQueryWrapper.lambda().eq(ZXGL_DuBanRenWuEntity::getZzr, zXGL_DuBanRenWuPagination.getZzr());
        }

        if (AllIdList.size() > 0) {
            zXGL_DuBanRenWuQueryWrapper.lambda().in(ZXGL_DuBanRenWuEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_DuBanRenWuPagination.getSidx())) {
            zXGL_DuBanRenWuQueryWrapper.lambda().orderByDesc(ZXGL_DuBanRenWuEntity::getFqr);
        } else {
            try {
                String sidx = zXGL_DuBanRenWuPagination.getSidx();
                ZXGL_DuBanRenWuEntity zXGL_DuBanRenWuEntity = new ZXGL_DuBanRenWuEntity();
                Field declaredField = zXGL_DuBanRenWuEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_DuBanRenWuQueryWrapper = "asc".equals(zXGL_DuBanRenWuPagination.getSort().toLowerCase()) ? zXGL_DuBanRenWuQueryWrapper.orderByAsc(value) : zXGL_DuBanRenWuQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_DuBanRenWuEntity> page = new Page<>(zXGL_DuBanRenWuPagination.getCurrentPage(), zXGL_DuBanRenWuPagination.getPageSize());
                IPage<ZXGL_DuBanRenWuEntity> userIPage = this.page(page, zXGL_DuBanRenWuQueryWrapper);
                return zXGL_DuBanRenWuPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_DuBanRenWuEntity> list = new ArrayList();
                return zXGL_DuBanRenWuPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_DuBanRenWuQueryWrapper);
        }
    }

    @Override
    public ZXGL_DuBanRenWuEntity getInfo(String id) {
        QueryWrapper<ZXGL_DuBanRenWuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_DuBanRenWuEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_DuBanRenWuEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_DuBanRenWuEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public void delete(ZXGL_DuBanRenWuEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}
